using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SSRSDeployer.ReportService2005;
using SSRSDeployer.Properties;

namespace SSRSDeployer
{
    public class ResourceDeployer : ReportingServiceDeployer
    {
        public ResourceDeployer(Dictionary<string, string> arguments) : base(arguments) { }

        protected override void ValidateArguments()
        {
            if (!File.Exists(ResourceLocation))
                throw new FileNotFoundException(string.Format(Resources.ResourceFileNotFound, ResourceLocation));
            if (string.IsNullOrEmpty(MimeType))
                throw new ArgumentNullException(Resources.NoMimeType);
        }

        public override void Deploy()
        {
            byte[] definition = null;
            using (FileStream fs = File.OpenRead(ResourceLocation))
            {
                definition = new byte[fs.Length];
                fs.Read(definition, 0, definition.Length);
                fs.Close();
            }
            if (CheckIfExists(ItemTypeEnum.Resource, Parent, ResourceName))
                DeleteItem(Parent, ResourceName);
            _rs.CreateResource(ResourceName, Parent, true, definition, MimeType, null);
            Console.WriteLine(Resources.ResourcePublishSuccess, ResourceName);
        }

        public string ResourceLocation
        {
            get { return Arguments["/irsc:"]; }
        }

        public string ResourceName
        {
            get { return Path.GetFileName(ResourceLocation); }
        }

        public string MimeType
        {
            get { return Arguments["/mimetype:"]; }
        }
    }
}
